--- title: "班级活动" created: 2025-11-28 tags: - 算法 --- # 班级活动 ## 题目 [班级活动](https://www.lanqiao.cn/paper/4103/problem/17153/) ![[image-7e051b92.png]] ## 思路分析 注意 题目是说 要成对出现 且同样的数存在两个 即否决了4 6 8这样的对 一开始没看到这一点 自以为是地用异或去写 ```cpp #include using namespace std; #define endl '\n' const int N=1e5+10; unordered_map a; int n; int main() { ios::sync_with_stdio(0),cin.tie(0),cout.tie(0); cin>>n; while(n--){ int x;cin>>x; a[x]^=1; } int cnt=0; for(auto x:a){ // cout< using namespace std; #define endl '\n' const int N=1e5+10; unordered_map all; int n; int main() { ios::sync_with_stdio(0),cin.tie(0),cout.tie(0); cin>>n; while(n--){ int x;cin>>x; all[x]++; } int a=0,b=0; for(auto x:all){ // cout<2) b+=x.second-2; } if(b>=a) cout< using namespace std; #define endl '\n' //多模拟几组案例可以发现 首先要把每个id记下数 //如果id小于2 说明肯定要改 或者等一个改成它 //如果id大于2 那么多出2的部分一定要改成其他的 // 1 2 2 2 2 2 3 4 // 22 3个2 分别改成 1 3 4 改三个数 这是多余部分(必须要改的部分) 恰好等于可能要改的部分的情况 // cost = 出现次数大于2的数量 // 1 2 2 2 3 4 // 22 1个2 改成1 3改成4 改两个数 这是多余部分(必须要改)的数 小于可能要改的数的情况 // cost = 出现次数大于2的数量 + 没被消耗完的 出现一次的数量 再除以2 // 1 1 2 2 2 2 // 11 22 多2个2 把22都改成33 改两个数 这是多余部分 大于 可能要改的部分的情况 //综上 先用值做下标计数 统计一下 出现一次的个数 以及 超过2部分的个数 // 若cnt2>=cnt1 则答案为cnt2 // 若cnt2>n; int maxv=0; for(int i=0;i>x; have[x]++; maxv=max(maxv,x); } int cnt1=0,cnt2=0; for(int i=0;i<=maxv;i++){ if(have[i]==1) cnt1++; if(have[i]>2){ cnt2+=have[i]-2; } } if(cnt2>=cnt1) cout<